﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using DotNetNuke.Entities.Tabs;
using DotNetNuke.Security.Permissions;
using System.Collections.Generic;
using DotNetNuke.Services.Localization;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Common;


namespace Satrabel.Services.WebsiteAudit
{
    public class WebsiteAuditUtils
    {

        public static List<StatPage> PageCheck(int PortalId) {
            var TabLst = new List<TabInfo>();
            var objTabs = new TabController();
            foreach (TabInfo objTab in objTabs.GetTabsByPortal(PortalId).Values)
            {
                if (!objTab.IsDeleted && !objTab.DisableLink && objTab.TabType == TabType.Normal && (Null.IsNull(objTab.StartDate) || objTab.StartDate < DateTime.Now) &&
                    (Null.IsNull(objTab.EndDate) || objTab.EndDate > DateTime.Now) && IsTabPublic(objTab.TabPermissions))
                {
                    TabLst.Add(objTab);
                }
            }

            var tabsWithTitles = TabLst.Where(t=> !string.IsNullOrEmpty(t.Title));
            var tabsWithDescriptions = TabLst.Where(t=> !string.IsNullOrEmpty(t.Description));


            var pages = new List<StatPage>();
            foreach (var tab in TabLst) {
                var stat = new StatPage()
                {
                    TabID = tab.TabID,
                    TabName = tab.TabName,
                    Culture = tab.CultureCode
                };
                
                if (string.IsNullOrEmpty(tab.Title))
                {
                    stat.Title =  "Empty";
                }                
                else if (tab.Title.Length >= 70)
                {
                    stat.Title =   "To long (> 69)";
                }
                else 
                {
                    int cntTitle = tabsWithTitles.Count(t => t.Title.ToLower() == tab.Title.ToLower());
                    if (cntTitle > 1)
                    stat.Title = "Duplicates (" + cntTitle + ")";
                }
                
                if (string.IsNullOrEmpty(tab.Description))
                {
                    stat.Description = "Empty";
                }               
                else if (tab.Description.Length >= 160)
                {
                    stat.Description = "To long (> 160)";
                }
                else 
                {
                    int cntDescription = tabsWithDescriptions.Count(t => t.Description.ToLower() == tab.Description.ToLower());
                    if (cntDescription > 1)
                        stat.Description = "Duplicates (" + cntDescription + ")";
                }
                if (!string.IsNullOrEmpty(stat.Title) || string.IsNullOrEmpty(stat.Description)) {
                    pages.Add(stat);
                }
            }


            return pages;               
        }

       

        public static bool IsTabPublic(TabPermissionCollection objTabPermissions)
        {
            string roles = objTabPermissions.ToString("VIEW");
            bool hasPublicRole = false;


            if ((roles != null))
            {
                // permissions strings are encoded with Deny permissions at the beginning and Grant permissions at the end for optimal performance
                foreach (string role in roles.Split(new[] { ';' }))
                {
                    if (!string.IsNullOrEmpty(role))
                    {
                        // Deny permission
                        if (role.StartsWith("!"))
                        {
                            string denyRole = role.Replace("!", "");
                            if ((denyRole == Globals.glbRoleUnauthUserName || denyRole == Globals.glbRoleAllUsersName))
                            {
                                hasPublicRole = false;
                                break;
                            }
                            // Grant permission
                        }
                        else
                        {
                            if ((role == Globals.glbRoleUnauthUserName || role == Globals.glbRoleAllUsersName))
                            {
                                hasPublicRole = true;
                                break;
                            }
                        }
                    }
                }
            }

            return hasPublicRole;
        }


    }

    public class StatPage
    {
        public int TabID { get; set; }
        public string Culture { get; set; }
        public string TabName { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public String Meta { get; set; }
        public string Raison { get; set; }
        public int Count { get; set; }

    }
}
